package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.httpUtils.HttpHeadParams;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.javaBean.UserLogin;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


public class LoginActivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private Button ok_button;
    private EditText edit_phone_number, edit_password;
    private TextView forget_password,tv_headtitle;
    private String phone_number, password, user_id,token;
    private ImageView regist_back;
    private Context context;
    private MyToast myToast;
    private ContributorPresenter contributorPresenter;
    private final String USER_LOGIN="USER_LOGIN";
    private final String MY_MODELU_INFO="MY_MODELU_INFO";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        context=this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
    }

    private void initView() {
        regist_back = (ImageView) findViewById(R.id.regist_back);
        ok_button = (Button) findViewById(R.id.ok_button);
        regist_back.setOnClickListener(this);
        ok_button.setOnClickListener(this);
        edit_phone_number = (EditText) findViewById(R.id.edit_phone_number);
        edit_password = (EditText) findViewById(R.id.edit_password);
        forget_password = (TextView) findViewById(R.id.forget_password);
        forget_password.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("登陆");
    }
    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.ok_button:
                phone_number=edit_phone_number.getText().toString().trim();
                password=edit_password.getText().toString().trim();
                if (phone_number.isEmpty()) {
                    myToast.showShort("手机号不能为空!");
                } else if(password.isEmpty()){
                    myToast.showShort("密码不能为空");
                }else
                {
                    contributorPresenter.getUserLogin(phone_number,password,USER_LOGIN);
                }
                break;
            case R.id.forget_password:
                Intent intent=new Intent(context,ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {
        if(flag.equals(USER_LOGIN))
        {
            ok_button.setEnabled(false);
        }
    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(USER_LOGIN)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10400"))
                {
                    myToast.showShort("用户不存在");
                }else if(code.equals("10801"))
                {
                    myToast.showShort("密码错误");
                }else if(code.equals("10802"))
                {
                    myToast.showShort("用户被禁用");
                }
                else if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    String messageString=JsonTools.objectToJsonString(message);
                    UserLogin userLogin=JsonTools.jsonStringToObject(messageString,UserLogin.class);
                    user_id=userLogin.getUserId();
                    token=userLogin.getToken();
                    Tools.setSharedPreferencesboolen(Constant.isLoginIn,true,context);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferencePhone,phone_number,context);
                    HttpHeadParams.getInstance().setMobile(phone_number);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferenceUserid,user_id,context);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferenceToken,token,context);
                    contributorPresenter.getMyModuleInfo(user_id,MY_MODELU_INFO);
                }
            }
            else if(flag.equals(MY_MODELU_INFO))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200"))
                {
                    Object message=respObj.getMessage();
                    String messageJson=JsonTools.objectToJsonString(message);
                    Tools.setSharedPreferencesStrings(Constant.SharePreferenceMyModuleInfo,messageJson,context);
                    Tools.setSharedPreferencesboolen(Constant.isFirstIn, false, context);
                    setResult(1);
                    finish();
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
        if(flag.equals(USER_LOGIN))
        {
            ok_button.setEnabled(true);
        }
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }
}
